package cn.bobohost.crmrbac.service.impl;

import cn.bobohost.crmrbac.pojo.po.Customer;
import cn.bobohost.crmrbac.dao.mapper.CustomerMapper;
import cn.bobohost.crmrbac.pojo.po.Employee;
import cn.bobohost.crmrbac.pojo.query.CustomerQuery;
import cn.bobohost.crmrbac.pojo.query.EchartsQuery;
import cn.bobohost.crmrbac.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 波波老师
 * @since 2025-05-21
 */
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional(readOnly = true)
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    //注入mapper
    private final CustomerMapper customerMapper;

    @Override
    public PageInfo<Customer> selectPotentialCustomerByCondition(CustomerQuery customerQuery) {

        //获取当前用户
        Employee user = (Employee) SecurityUtils.getSubject().getPrincipal();
        //将其放到查询条件中:只查询当前登录用户所销售联系的潜在客户
        customerQuery.setSellerId(user.getId());


        PageHelper.startPage(customerQuery.getCurrentPage(), customerQuery.getPageSize());
        List<Customer> list = customerMapper.selectPotentialCustomerByCondition(customerQuery);


        return new PageInfo<>(list);
    }


    @Override
    public PageInfo<EchartsQuery> getEchartsQueryList(CustomerQuery customerQuery,String keyword,String dateBegin,String dateEnd) {
        PageHelper.startPage(customerQuery.getCurrentPage(), customerQuery.getPageSize());

        Map<String, Object> map = countCustomerMake(customerQuery.getGroupType(),keyword,dateBegin,dateEnd);

        List<EchartsQuery> list = new ArrayList<>();
        ArrayList<Object> names = (ArrayList<Object>) map.get("data1");
        String[] name = new String[names.size()];
        ArrayList<Object> nums = (ArrayList<Object>) map.get("data2");
        Integer[] num = new Integer[nums.size()];
        Iterator<Object> itNames = names.iterator();
        Iterator<Object> itNums = nums.iterator();
        int i = 0;
        int j = 0;
        while (itNames.hasNext()) {
            String nameFor = (String) itNames.next();
            name[i++] = nameFor;
        }
        while (itNums.hasNext()) {
            Integer numFor = (Integer) itNums.next();
            num[j++] = numFor;
        }

        for (int k = 0; k < name.length; k++) {
            EchartsQuery query = new EchartsQuery();
            query.setName(name[k]);
            query.setNum(num[k]);
            list.add(query);
        }
        return new PageInfo<>(list);
    }




    /**
     * 查询潜在客户报表
     *
     * @return
     */
    @Override
    public Map<String, Object> countCustomerMake(Integer groupType,String keyword,String dateBegin,String dateEnd) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> dataList = new ArrayList<>();
        //查询客户构成数据的列表
        switch (groupType) {
            case 0:
                dataList = customerMapper.countCustomerMakeByEmp(keyword,dateBegin,dateEnd);
                break;
            case 1:
                dataList = customerMapper.countCustomerMakeByYear(keyword,dateBegin,dateEnd);
                break;
            case 2:
                dataList = customerMapper.countCustomerMakeByMonth(keyword,dateBegin,dateEnd);
                break;
            case 3:
                dataList = customerMapper.countCustomerMakeByDay(keyword,dateBegin,dateEnd);
                break;
            default:
                System.out.println("未查到对应类型的数据及图表信息！");
        }

        //柱状图X轴的数据(目前暂为员工名) 数组
        List<String> data1 = new ArrayList<>();
        //柱状图Y轴的数据(新增客户数)  数组
        List<Integer> data2 = new ArrayList<>();

        //判断数据列表，循环设置数据
        if (dataList != null && dataList.size() > 0) {
            //遍历集合
            dataList.forEach(m -> {
                //获取name对应的数据，设置到X轴的集合中
                data1.add(m.get("groupType").toString());
                //获取total对应的数据，设置到Y轴的集合中
                data2.add(Integer.parseInt(m.get("total").toString()));
            });
        }
        //将X轴的数据和Y轴的数据添加到map集合中
        map.put("data1", data1);
        map.put("data2", data2);
        return map;
    }

}
